<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/include/commonTaglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色管理</title>
<script type="text/javascript">
$(function(){
	//角色列表
	$("#roleListGrid").datagrid({  
	      url:"${ctx}/permission/getRolePage",
	      width:'99%',
	      height:'99%',
	      pagination:true,//显示分页  
	      rownumbers:true,//显示行号
	      pageSize:20,//分页大小  
	      pageList:[20,40,60,80],//每页的个数  
	      //fit:true,//自动补全  
	      fitColumns:true, 
	      			loadFilter : function(data){
	      				 //过滤数据
	      				if(data.items==null || data.items==""){
	       					return {total:0,rows:[]}; 
	       				}else{
	      				 var value={
	      				 total:data.total,
	      				 rows:[]
	      				 };
	      				 var x=0;
	      				 for (var i = 0; i < data.items.length; i++) {  
	      					 value.rows[x]=data.items[i];
	      					 x++;
	      				 }
	      				 return value;
	       				}
	      			},
	      toolbar:"#toolbar", 
	      columns:[[      //每个列具体内容  
	               {field:'ck',checkbox:true },
	               {field:'id', title:'id',width:100,hidden:true},
	               {field:'name', title:'角色名',width:100},
	               {field:'remark', title:'角色描述',width:100}
	            ]],
	             singleSelect: false,
	 	         selectOnCheck: true,
	 	         checkOnSelect: true,
	  });
	//权限列表
	$("#permissionListGrid").datagrid({  
	      url:"${ctx}/permission/getPermissionPage",
	      width:'99%',
	      height:'99%',
	      pagination:false,//显示分页  
	      rownumbers:true,//显示行号
	      //fit:true,//自动补全  
	      fitColumns:true, 
	      			loadFilter : function(data){
	      				 //过滤数据
	      				if(data.items==null || data.items==""){
	       					return [] 
	       				}else{
	      				 var value=[];
	      				 var x=0;
	      				 for (var i = 0; i < data.items.length; i++) {  
	      					 value[x]=data.items[i];
	      					 x++;
	      				 }
	      				 return value;
	       				}
	      			},
	      columns:[[      //每个列具体内容  
	               {field:'ck',checkbox:true },
	               {field:'id', title:'id',width:100,hidden:true},
	               {field:'name', title:'角色名',width:100},
	               {field:'remark', title:'角色描述',width:100}
	            ]],
	             singleSelect: false,
	 	         selectOnCheck: true,
	 	         checkOnSelect: true,
	 	         onLoadSuccess:function(data){
	 	        	//初始化设置权限
	 	     	   $("#setPermission").dialog({
	 	     		  closed:true,
	 	     		  buttons: [{
	 	     				text:'提交',
	 	     				iconCls:'icon-ok',
	 	     				handler:function(){
	 	     				   setPermissionSumbit();	
	 	     				}
	 	     			},{
	 	     				text:'取消',
	 	     				iconCls:'icon-cancel',
	 	     				handler:function(){
	 	     					$('#setPermission').dialog('close');
	 	     				}
	 	     			}]
	 	     	  })
	 	         }
	  });
	
	//设置分页
	 var p = $('#roleListGrid').datagrid('getPager');  
	  $(p).pagination({  
	      beforePageText: '第',//页数文本框前显示的汉字  
	      afterPageText: '页    共 {pages} 页',  
	      displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',  
	   });
	  //初始化菜单列表
	  $('#roleMenuList').tree({
			url: '${ctx}/menu/getAllMenu.do',
			cascadeCheck:false,
			checkbox:true,
			lines:true,
			loadFilter: function(rows){
				if(null!=rows&&null!=rows.children){
					rows=rows.children;
				 }
				return rows;
			},
			onLoadSuccess: function (node,data) {
			    if (data.length > 0) {
			          /* //找到第一个元素
			          var n = $('#roleMenuList').tree('find', data[0].id);
			          //调用选中事件
			          $('#roleMenuList').tree('check', n.target); */
			    }
			      }, 
		});
	  //初始化弹出框
	  $("#popup").dialog({
		  title:'新增',
		  closed:true,
		  buttons: [{
				text:'提交',
				iconCls:'icon-ok',
				handler:function(){
				   sumbit();	
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#popup').dialog('close');
				}
			}]
	  })
	  //初始化设置权限
	   $("#setMenu").dialog({
		  closed:true,
		  buttons: [{
				text:'提交',
				iconCls:'icon-ok',
				handler:function(){
				   setMenuSumbit();	
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#setMenu').dialog('close');
				}
			}]
	  })
})
 
//搜索
function search(){
	$('#roleListGrid').datagrid('load', {
	    name: $("#roleName").textbox("getValue"),
	});
}
//设置菜单
function setMenu(){
	var oldCheckedNode=$('#roleMenuList').tree("getChecked");
	for(var i=0;i<oldCheckedNode.length;i++){
		$('#roleMenuList').tree("uncheck",oldCheckedNode[i].target);
	}
	var checkdeRow=$('#roleListGrid').datagrid("getSelections");
	if(checkdeRow.length==0){
		$.messager.alert("提示","请选择一行！");
		return;
	}
	if(checkdeRow.length>1){
		$.messager.alert("提示","只能选中一行！");
		return;
	}
	var roleId=checkdeRow[0].id;
	$.ajax({
		url: '${ctx}/permission/getRoleAndMenu',
		data:{roleId:roleId},
		dataType:'JSON',
		Type:'POST',
		success:function(data){
			$("#setMenu").dialog("open");
			for(var i=0;i<data.length;i++){
				var node = $('#roleMenuList').tree('find',data[i].menuId);
				if(node){
					$('#roleMenuList').tree('check', node.target);
				}
			}
		},
		error:function(data){
			$.messager.alert("提示","获取数据失败！","error");
		}
		
	});
}
//设置菜单提交
function setMenuSumbit(){
	var checkedNode=$('#roleMenuList').tree('getChecked');
	var checkedNodeId;
	for(var  i=0;i<checkedNode.length;i++){
		if(i==0){
			checkedNodeId=checkedNode[i].id;	
		}else{
			checkedNodeId+=","+checkedNode[i].id	
		}
	}
	var checkdeRow=$('#roleListGrid').datagrid("getSelections");
	var roleId=checkdeRow[0].id;
	$.ajax({
		url:'${ctx}/permission/setMenuPermission',
		data:{roleId:roleId,checkedMenuId:checkedNodeId},
		dataType:'JSON',
		TYPE:'POST',
		success:function(data){
			$.messager.alert("提示","设置成功！");
			$("#setMenu").dialog("close");
		},
		error:function(data){
			$.messager.alert("提示","未知错误请联系管理员！","error");
		}
	})
}
//设置权限
function setPermission(){
	$("#permissionListGrid").datagrid("uncheckAll");
	var checkdeRow=$('#roleListGrid').datagrid("getSelections");
	if(checkdeRow.length==0){
		$.messager.alert("提示","请选择一行！");
		return;
	}
	if(checkdeRow.length>1){
		$.messager.alert("提示","只能选中一行！");
		return;
	}
	var roleId=checkdeRow[0].id;
	$.ajax({
		url: '${ctx}/permission/getRoleAndPermissions',
		data:{roleId:roleId},
		dataType:'JSON',
		Type:'POST',
		success:function(data){
			var rows=$("#permissionListGrid").datagrid("getRows");
			if(data){
				for(var i=0;i<data.length;i++){
					for(var j=0;j<rows.length;j++){
						if(data[i].permissionId==rows[j].id){
							$("#permissionListGrid").datagrid("checkRow",j);
							break;
						}	
					}
				}
			}
			$('#setPermission').dialog('open');
		},
		error:function(data){
			$.messager.alert("提示","获取数据失败！","error");
		}
	});
}
//设置权限提交
function setPermissionSumbit(){
	var checkedNode=$('#permissionListGrid').datagrid("getSelections");
	var checkedNodeId;
	for(var  i=0;i<checkedNode.length;i++){
		if(i==0){
			checkedNodeId=checkedNode[i].id;	
		}else{
			checkedNodeId+=","+checkedNode[i].id	
		}
	}
	var checkdeRow=$('#roleListGrid').datagrid("getSelections");
	var roleId=checkdeRow[0].id;
	$.ajax({
		url:'${ctx}/permission/setPermission',
		data:{roleId:roleId,checkedPermissionId:checkedNodeId},
		dataType:'JSON',
		TYPE:'POST',
		success:function(data){
			$.messager.alert("提示","设置成功！");
			$("#setPermission").dialog("close");
		},
		error:function(data){
			$.messager.alert("提示","未知错误请联系管理员！","error");
		}
	})
}
//新增按钮
function add(){
	$("#type").val("0");
	$("#id").val("");
	$("#name").textbox("setValue","");
	$("#remark").textbox("setValue","");
	$("#popup").dialog("setTitle","新增");
	$('#popup').dialog('open');
}
//修改按钮
function edit(){
	var checkdeRow=$('#roleListGrid').datagrid("getSelections");
	if(checkdeRow.length==0){
		$.messager.alert("提示","请选择一行！");
		return;
	}
	if(checkdeRow.length>1){
		$.messager.alert("提示","只能选中一行！");
		return;
	}
	var id=checkdeRow[0].id;
	var name=checkdeRow[0].name;
	var remark=checkdeRow[0].remark;
	$("#type").val("1");
	$("#id").val(id);
	$("#name").textbox("setValue",name);
	$("#remark").textbox("setValue",remark);
	$("#popup").dialog("setTitle","修改");
	$('#popup').dialog('open');
}
//删除
function deleteRole(){
	var checkdeRow=$('#roleListGrid').datagrid("getSelections");
	if(checkdeRow.length==0){
		$.messager.alert("提示","请选择一行！");
		return;
	}
	var names;
	for(var  i=0;i<checkdeRow.length;i++){
		if(i==0){
			names=checkdeRow[i].name;	
		}else{
			names+=","+checkdeRow[i].name	
		}
	}
	$.messager.confirm('提示', "你要删除<span style='color:red'>"+names+"</span>?", function(r){
		if (r){
			var ids;
			for(var  i=0;i<checkdeRow.length;i++){
				if(i==0){
					ids=checkdeRow[i].id;	
				}else{
					ids+=","+checkdeRow[i].id	
				}
			}
			$.ajax({
				url:'${ctx}/permission/deleteRoles',
				data:{ids:ids},
				dataType:"JSON",
				type:"POST",
				success:function(data){
					$.messager.alert("提示","删除成功！");
					$('#roleListGrid').datagrid('reload'); 
				},
				error:function(data){
					$.messager.alert("提示","未知错误，请联系管理员！","error");
				}
			})
			
		}
	});
}
//新增和修改提交
function sumbit(){
	var id=$("#id").val();
	var type=$("#type").val();
	var name=$("#name").textbox("getValue");
	var remark=$("#remark").textbox("getValue");
	$.ajax({
		url:'${ctx}/permission/saveRole',
		data:{
			  type:type,
			  id:id,
			  name:name,
			  remark:remark},
		dataType:"JSON",
		type:"POST",
		success:function(data){
		$.messager.alert("提示",data.info);
		if(data.serialNumber=="-1"){
			return;	
		}
		$('#roleListGrid').datagrid('reload'); 
		$('#popup').dialog('close');
		},
		error:function(data){
			$.messager.alert("提示","未知错误请联系管理员！","error");
		}
	})
}
</script>
</head>
<body>
   <table id="roleListGrid" class="easyui-datagrid" title="角色列表">
   </table>
   <div id="toolbar" style="padding:5px;height:auto">
	<div style="margin-bottom:5px">
		<a href="javascript:add()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
		<a href="javascript:edit()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
		<a href="javascript:setMenu()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">设置菜单</a>
		<a href="javascript:setPermission()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">设置权限</a>
		<a href="javascript:deleteRole()" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">删除</a>
	</div>
	<div>
		角色名:<input id="roleName"class="easyui-textbox" style="width:80px">
		<a href="javascript:search()" class="easyui-linkbutton" iconCls="icon-search">Search</a>
	</div>
	<div id="popup"  class="easyui-dialog"  style="width:400px;height:200px;padding:10px">
	     <table style="text-align:center;width:90%">
	        <tr>
	           <td><lable for="name">名称:</lable></td>
	           <td><input  id="name" name="name" class="easyui-textBox"/></td>
	         </tr>
	         <tr>
	           <td><lable for="remark">描述:</lable></td>
	           <td><input  id="remark" name="remark" class="easyui-textBox"/></td>
	         </tr>
	     </table>
	     <input  id="id" type="text"style="width:200px;display:none">
	     <input  id="type" type="text"style="width:200px;display:none">
	</div>
	<div id="setMenu" title="设置菜单" class="easyui-dialog"  style="width:400px;height:400px;padding:10px">
	    <ul id="roleMenuList" class="easyui-tree">
	    </ul>
	</div>
	<div id="setPermission" title="设置权限" class="easyui-dialog"  style="width:800px;height:400px;padding:10px">
	    <table id="permissionListGrid" class="easyui-datagrid" title="权限列表"></table>
	</div>
</div>
</body>
</html>